const ls = window.localStorage
const ss = window.sessionStorage

export function isArray (arr) {
  if (!Array.isArray) {
    Array.isArray = (arg) => {
      return Object.prototype.toString.call(arg) === '[object Array]'
    }
  }
  return Array.isArray(arr)
}

export function isObject (obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

export function isEmpty (str) {
  // eslint-disable-next-line
  if (typeof str === 'undefined' || str === null || !(str === str) || str.toString().includes('NaN')) str = ''
  const reg = new RegExp(/^[\s]*$/)
  str = trim(str)
  return reg.test(str)
}

export function isNumber (val) {
  return typeof val === 'number'
}

export function isJSON (str) {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str)
      if (typeof obj === 'object' && obj) {
        return true
      } else {
        return false
      }
    } catch (e) {
      console.log('error：' + str + '!!!' + e)
      return false
    }
  }
  console.warn('It is not a string!')
}

export function trim (str) {
  if (String.prototype.trim) {
    return str === null ? '' : String.prototype.trim.call(str)
  } else {
    return str.replace(/(^\s*)|(\s*$)/g, '')
  }
}

/**
 * 去除字符串中所有空格
 * @param str
 * @returns {string | void | *}
 */
export function trimAll (str) {
  return str.replace(/\s*/g, '')
}

export function changeNumToHan (num) {
  var arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  var arr2 = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']
  if (!num || isNaN(num)) return '零'
  var english = num.toString().split('')
  var result = ''
  for (var i = 0; i < english.length; i++) {
    var des_i = english.length - 1 - i// 倒序排列设值
    result = arr2[i] + result
    var arr1_index = english[des_i]
    result = arr1[arr1_index] + result
  }
  result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十') // 将【零千、零百】换成【零】 【十零】换成【十】
  result = result.replace(/零+/g, '零') // 合并中间多个零为一个零
  result = result.replace(/零亿/g, '亿').replace(/零万/g, '万') // 将【零亿】换成【亿】【零万】换成【万】
  result = result.replace(/亿万/g, '亿') // 将【亿万】换成【亿】
  result = result.replace(/零+$/, '') // 移除末尾的零
  // 将【一十】换成【十】
  result = result.replace(/^一十/g, '十')
  return result
}

export const setLocal = (key, value) => {
  if (ls && key && value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
      value = 'obj-' + value
    } else {
      value = 'str-' + value
    }
    ls.setItem(key, value)
  }
}

export const getLocal = key => {
  if (ls) {
    let v = ls.getItem(key)
    if (!v) return
    if (v.indexOf('obj-') === 0) {
      v = v.slice(4)
      return JSON.parse(v)
    } else if (v.indexOf('str-') === 0) {
      return v.slice(4)
    }
  }
}

export const rmLocal = key => {
  if (ls && key) {
    ls.removeItem(key)
  }
}

export const clearLocal = () => {
  ls && ls.clear()
}

export const setSession = (key, value) => {
  if (ss && key && value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
      value = 'obj-' + value
    } else {
      value = 'str-' + value
    }
    ss.setItem(key, value)
  }
}

export const getSession = key => {
  if (ss) {
    let v = ss.getItem(key)
    if (!v) return
    if (v.indexOf('obj-') === 0) {
      v = v.slice(4)
      return JSON.parse(v)
    } else if (v.indexOf('str-') === 0) {
      return v.slice(4)
    }
  }
}

export const rmSession = key => {
  if (ss && key) {
    ss.removeItem(key)
  }
}

export const clearSession = () => {
  ss && ss.clear()
}
